package com.yzt.dynamicmodule.presenter;

import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.DynamicCommentBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicListBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgBaseBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgChildBean;
import com.yzt.zhuju.network.entity.user.FollowUserStatusRequest;
import com.yzt.dynamicmodule.contacts.DynamicDetailContacts;
import com.yzt.plugin_network.response.HttpResponse;

import java.util.ArrayList;
import java.util.List;

public class DynamicDetailPresenter extends DynamicDetailContacts.Presenter {

    private String dynamicId;

    @Override
    public void setDynamicId(String dynamicId) {
        this.dynamicId = dynamicId;
    }

    @Override
    public void getDetails() {
        BizLayer.getInstance().getDynamicModule().dynamicDetail(dynamicId, new AresSubscriber<DynamicListBean>() {
            @Override
            protected void onSuccess(DynamicListBean response) {
                mView.loadDynamicDetail(response);
            }
        });
    }

    @Override
    public void getCommentList() {
        BizLayer.getInstance().getDynamicModule().dynamicCommentList(dynamicId, 100, 1, new AresSubscriber<DynamicMsgBaseBean>() {
            @Override
            protected void onSuccess(DynamicMsgBaseBean response) {
                mView.loadCommentList(response.getCommentList());
            }
        });
    }

    @Override
    public void delDynamic() {
        BizLayer.getInstance().getDynamicModule().dynamicDel(dynamicId, new AresSubscriber<HttpResponse>() {
            @Override
            protected void onSuccess(HttpResponse httpResponse) {
                mView.delDynamic();
            }
        });
    }

    /**
     * 添加评论
     */
    @Override
    public void addComment(DynamicCommentBean bean) {
        if (bean.getStatus() == 1) {
            BizLayer.getInstance().getDynamicModule().dynamicCommentAdd(dynamicId, bean, new AresSubscriber<DynamicMsgBean>() {
                @Override
                protected void onSuccess(DynamicMsgBean response) {
                    //  mView.dynamicReply(bean, response);
                    getCommentList();
                    // TODO: 2023/9/5
                }
            });
        } else {
            BizLayer.getInstance().getDynamicModule().dynamicCommentAddChild(dynamicId, bean, new AresSubscriber<DynamicMsgChildBean>() {
                @Override
                protected void onSuccess(DynamicMsgChildBean response) {
                    getCommentList();
                    // mView.dynamicReplyChild(bean, response);
                }
            });
        }
    }

    /**
     * 动态点赞
     */
    @Override
    public void dynamicLike(boolean like) {
        if (like) {
            BizLayer.getInstance().getDynamicModule().dynamicLike(dynamicId, new AresSubscriber<HttpResponse>() {
                @Override
                protected void onSuccess(HttpResponse response) {
                    mView.dynamicLike(like);
                }
            });
        } else {
            BizLayer.getInstance().getDynamicModule().dynamicUnLike(dynamicId, new AresSubscriber<HttpResponse>() {
                @Override
                protected void onSuccess(HttpResponse response) {
                    mView.dynamicLike(like);
                }
            });
        }
    }

    /**
     * 动态评论点赞
     *
     * @param dynamicId1     动态ID
     * @param type           1 评论 2 子评论
     * @param commentId      评论id
     * @param childCommentId 子评论id
     * @param status         点赞状态 true 点赞 false 取消点赞
     */
    @Override
    public void dynamicCommentLike(int type, String dynamicId1, String commentId, String childCommentId, boolean status) {
        BizLayer.getInstance().getDynamicModule().dynamicCommentLike(type, dynamicId, commentId, childCommentId, status, new AresSubscriber<HttpResponse>() {
            @Override
            protected void onSuccess(HttpResponse response) {
                mView.dynamicCommentLike(type, dynamicId, commentId, childCommentId, status);
            }
        });
    }

    /**
     * 关注用户
     */
    @Override
    public void followUser(String userId) {
        FollowUserStatusRequest mFollowRequest = new FollowUserStatusRequest();
        List<String> list = new ArrayList<>();
        list.add(userId);
        mFollowRequest.setUidRels(list);
        mFollowRequest.setStatus(1);
        BizLayer.getInstance().getAccountModule().postFollowOrUnfollowUser(mFollowRequest, new AresSubscriber<HttpResponse>() {
            @Override
            protected void onSuccess(HttpResponse response) {
                mView.followUser();
            }
        });
    }
}
